package com.guigu.service.impl;

import com.guigu.dao.EmployeeMapper;
import com.guigu.domain.Employee;
import com.guigu.domain.EmployeeExample;
import com.guigu.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService {

    @Qualifier("employeeMapper")
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public List<Employee> getAllEmp() {
        return employeeMapper.selectByExampleWithDept (null);
    }

    @Override
    public void addEmp(Employee employee) {
        employeeMapper.insertSelective (employee);
    }

    @Override
    public Employee getEmpByName(String empName) {
        try {
   /*         EmployeeExample example = new EmployeeExample ();
            EmployeeExample.Criteria criteria = example.createCriteria ();
            criteria.andEmpNameEqualTo (empName);
            List<Employee> employees = employeeMapper.selectByExample (example);*/
            Employee employee = employeeMapper.selectByEmpName (empName);
            return employee;
        }catch (Exception e){
            return null;
        }

    }

    @Override
    public Employee getEmp(Integer empId) {
        return employeeMapper.selectByPrimaryKey (empId);
    }

    @Override
    public void updateEmp(Employee employee) {
        employeeMapper.updateByPrimaryKeySelective (employee);
    }

    @Override
    public void delEmp(Integer empId) {
        employeeMapper.deleteByPrimaryKey (empId);
    }

    @Override
    public void batchDelEmp(List<Integer> ids) {
        EmployeeExample example = new EmployeeExample ();
        EmployeeExample.Criteria criteria = example.createCriteria ();
        //delete from tab_emp where emp_id in (ids)
        criteria.andEmpIdIn (ids);
        employeeMapper.deleteByExample (example);
    }
}
